package com.delamer.facile.db;

import com.delamer.facile.obj.ObjectWarp;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBUtilWarp {
    private static final String JDBC_URL = "jdbc:h2:" + System.getProperty("user.dir") + "/plugins/DelamerFacile/database/df_warp";
    private static final String USERNAME = "yn89b5";
    private static final String PASSWORD = "9iqx7f";
    private static final String DRIVER = "org.h2.Driver";
    private static Connection connection = null;
    private static Statement statement = null;
    private static final String INIT = "CREATE TABLE IF NOT EXISTS df_warp("
            + "warp_name VARCHAR(50) NOT NULL,"
            + "world_name VARCHAR(50) NOT NULL,"
            + "location_x VARCHAR(50) NOT NULL,"
            + "location_y VARCHAR(50) NOT NULL,"
            + "location_z VARCHAR(50) NOT NULL,"
            + "is_del TINYINT NOT NULL DEFAULT 0,"
            + "create_time DATETIME,"
            + "update_time DATETIME,"
            + "id INT NOT NULL PRIMARY KEY AUTO_INCREMENT);";

    public static void init() {
        if (statement == null)
            try {
                open();
                statement.execute(INIT);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void open() {
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
            statement = connection.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void close() {
        try {
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void close(Statement statement) {
        try {
            Connection connection = statement.getConnection();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static ResultSet select(String sql) {
        try {
            ResultSet resultSet = statement.executeQuery(sql);
            return resultSet;
        } catch (Exception e) {
            e.printStackTrace();
            close();
            return null;
        }
    }

    private static int insert(String sql) {
        try {
            int count = statement.executeUpdate(sql);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            close();
            return 0;
        }
    }

    private static int update(String sql) {
        try {
            int count = statement.executeUpdate(sql);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            close();
            return 0;
        }
    }

    private static int delete(String sql) {
        try {
            int count = statement.executeUpdate(sql);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            close();
            return 0;
        }
    }

    public static ObjectWarp getWarp(String warpName) throws SQLException {
        ResultSet rs = select("SELECT * FROM df_warp WHERE is_del = 0 and warp_name = '" + warpName + "';");
        List<ObjectWarp> list = new ArrayList<ObjectWarp>();
        if (rs != null)
            while (rs.next()) {
                ObjectWarp object = new ObjectWarp() {{
                    setWarpName(rs.getString("warp_name"));
                    setWorldName(rs.getString("world_name"));
                    setLocationX(rs.getString("location_x"));
                    setLocationY(rs.getString("location_y"));
                    setLocationZ(rs.getString("location_z"));
                }};
                list.add(object);
            }
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    public static List<ObjectWarp> getWarpList() throws SQLException {
        ResultSet rs = select("SELECT * FROM df_warp WHERE is_del = 0;");
        List<ObjectWarp> list = new ArrayList<ObjectWarp>();
        if (rs != null)
            while (rs.next()) {
                ObjectWarp object = new ObjectWarp() {{
                    setWarpName(rs.getString("warp_name"));
                    setWorldName(rs.getString("world_name"));
                    setLocationX(rs.getString("location_x"));
                    setLocationY(rs.getString("location_y"));
                    setLocationZ(rs.getString("location_z"));
                }};
                list.add(object);
            }
        return list;
    }

    public static boolean addWarp(ObjectWarp object) throws SQLException {
        String sql = "INSERT INTO df_warp(warp_name,world_name,location_x,location_y,location_z,create_time) VALUES("
                + "'" + object.getWarpName() + "',"
                + "'" + object.getWorldName() + "',"
                + "'" + object.getLocationX() + "',"
                + "'" + object.getLocationY() + "',"
                + "'" + object.getLocationZ() + "',"
                + "CURRENT_DATE()"
                + ");";
        return insert(sql) > 0;
    }

    public static boolean delWarp(ObjectWarp object) {
        String sql = "UPDATE df_warp SET "
                + "is_del = 1,"
                + "update_time = CURRENT_DATE() "
                + "WHERE warp_name = '" + object.getWarpName() + "' "
                + "AND is_del = 0;";
        return delete(sql) > 0;
    }

    public static boolean setWarp(ObjectWarp object) {
        String sql = "UPDATE df_warp SET "
                + "world_name = '" + object.getWorldName() + "',"
                + "location_x = '" + object.getLocationX() + "',"
                + "location_y = '" + object.getLocationY() + "',"
                + "location_z = '" + object.getLocationZ() + "',"
                + "update_time = CURRENT_DATE() "
                + "WHERE warp_name = '" + object.getWarpName() + "' "
                + "AND is_del = 0;";
        return update(sql) > 0;
    }

}
